package com.fenghongzhang.day015;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    //放一个啥? 对象.
    private static final String TAG = "MyAdapter";
    private List<Food.DataBean> list;
    private Context context;
    public MyAdapter(List<Food.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }
    /**(
     * 一个这个list,有多少个
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i(TAG, "getView: ");
        VH vh;
        //view 就是每一行的布局.
        if(convertView == null){ //第一次
            //服用布局
            vh = new VH();
            convertView = LayoutInflater.from(context).inflate(R.layout.lv_layout, null);
//            TextView textView = convertView.findViewById(R.id.lv_text);
            vh.t = convertView.findViewById(R.id.lv_text);
            vh.img = convertView.findViewById(R.id.lv_img);
            convertView.setTag(vh);
        }else{
            //取地址值
             vh = (VH) convertView.getTag();
        }

        vh.t.setText(list.get(position).getTitle());
        vh.img.setImageResource(R.mipmap.ic_launcher_round);
        return convertView;
    }
    //vh里面是用来缓存findId地址值的,所以,布局文件中,有了那个组件,这个就要加什么组件
    class VH{
        TextView t;
        ImageView img;
    }
}
